package com.sunday.common.utils;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/1/4.
 */
public class TimeUtils {
    static StringBuffer result = new StringBuffer();

    public static String getTimeString(long milliseconds) {
        result.delete(0, result.length());
        long time = System.currentTimeMillis() - (milliseconds * 1000);
        long mill = (long) Math.ceil(time / 1000);//秒前
        long minute = (long) Math.ceil(time / 60 / 1000.0f);// 分钟前
        long hour = (long) Math.ceil(time / 60 / 60 / 1000.0f);// 小时
        long day = (long) Math.ceil(time / 24 / 60 / 60 / 1000.0f);// 天前
        if (day - 1 > 0 && day - 1 < 30) {
            result.append(day + "天");
        } else if (day - 1 >= 30) {
            result.append(Math.round((day - 1) / 30) + "个月");
        } else if (hour - 1 > 0) {
            if (hour >= 24) {
                result.append("1天");
            } else {
                result.append(hour + "小时");
            }
        } else if (minute - 1 > 0) {
            if (minute == 60) {
                result.append("1小时");
            } else {
                result.append(minute + "分钟");
            }
        } else if (mill - 1 > 0) {
            if (mill == 60) {
                result.append("1分钟");
            } else {
                result.append(mill + "秒");
            }
        } else {
            result.append("刚刚");
        }
        if (!result.toString().equals("刚刚")) {
            result.append("前");
        }
        return result.toString();
    }

    public static String getWeekOfDate(Date date) {
        String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekOfDays[w];

    }
}
